package com.qianli.cashmama.trade.integration.notification.message.utils;

import com.alibaba.fastjson.JSONObject;
import com.fqgj.common.api.Response;
import com.fqgj.msg.enums.AppEnum;
import com.fqgj.msg.enums.PushServicerTypeEnum;
import com.fqgj.msg.enums.PushTypeEnum;
import com.fqgj.msg.push.PushMsgService;
import com.fqgj.msg.ro.NoticePushRO;
import com.fqgj.msg.ro.PushMsgInfoRO;
import com.fqgj.msg.ro.SinglePushSendRequestRO;
import com.fqgj.xjd.user.client.UserBehaviorService;
import com.fqgj.xjd.user.client.UserService;
import com.fqgj.xjd.user.client.response.UserBehaviorRegister;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * ·
 *
 * @author liuming
 * Created on 上午11:26 2018/9/26.
 */
@Component
public class PushUtils {

    private static final Logger log = LoggerFactory.getLogger(PushUtils.class);

    //@MotanReferer(basicReferer = "basicRefererConfig", group = "motan_cashmama_user")
    @Autowired
    private UserService userService;
    //@MotanReferer(basicReferer = "basicRefererConfig", group = "motan_cashmama_user")
    @Autowired
    private UserBehaviorService userBehaviorService;
    //@MotanReferer(basicReferer = "basicRefererConfig", group = "motan_cashmama_message")
    @Autowired
    private PushMsgService pushMsgService;


    public void singlePush(String userCode, String title, String body, String jumpPage) {
        try {
            Response<UserBehaviorRegister> userBehaviorRegisterResponse = userBehaviorService.getRegisterByUserCode(userCode);
            UserBehaviorRegister data = userBehaviorRegisterResponse.getData();
            String clientId = data.getClientId();
            Response<String> openIdByUserCode = userService.getOpenIdByUserCode(userCode);
            String openId = null;
            if (openIdByUserCode != null) {
                openId = openIdByUserCode.getData();
            }
            SinglePushSendRequestRO singlePushSendRequestRO = new SinglePushSendRequestRO();
            singlePushSendRequestRO.setApp(AppEnum.JYD);
            singlePushSendRequestRO.setPushTypeEnum(PushTypeEnum.NOTICE);
            singlePushSendRequestRO.setPushServicerTypeEnum(PushServicerTypeEnum.FCM);
            singlePushSendRequestRO.setSerialNum(String.valueOf(System.currentTimeMillis()));
            PushMsgInfoRO pushMsgInfoRO = new PushMsgInfoRO();
            pushMsgInfoRO.setUserCode(userCode);
            pushMsgInfoRO.setClientId(clientId);
            NoticePushRO noticePushRO = new NoticePushRO();
            noticePushRO.setTitle(title);
            noticePushRO.setBody(body);
            noticePushRO.setJumpPage(jumpPage);
            noticePushRO.setReceiver(openId);
            noticePushRO.setClientId(clientId);
            pushMsgInfoRO.setContent(JSONObject.toJSONString(noticePushRO));
            singlePushSendRequestRO.setMsgInfoRO(pushMsgInfoRO);
            pushMsgService.singlePush(singlePushSendRequestRO);
        } catch (Exception e) {
            log.error("singlePush,push message exception", e);
        }
    }

    public void singleNotice(String userCode, String title, String body, String jumpPage) {
        try {
            Response<UserBehaviorRegister> userBehaviorRegisterResponse = userBehaviorService.getRegisterByUserCode(userCode);
            UserBehaviorRegister data = userBehaviorRegisterResponse.getData();
            String clientId = data.getClientId();
            Response<String> openIdByUserCode = userService.getOpenIdByUserCode(userCode);
            String openId = null;
            if (openIdByUserCode != null) {
                openId = openIdByUserCode.getData();
            }
            SinglePushSendRequestRO singlePushSendRequestRO = new SinglePushSendRequestRO();
            singlePushSendRequestRO.setApp(AppEnum.JYD);
            singlePushSendRequestRO.setPushTypeEnum(PushTypeEnum.NOTICE);
            singlePushSendRequestRO.setPushServicerTypeEnum(PushServicerTypeEnum.FCM);
            singlePushSendRequestRO.setSerialNum(String.valueOf(System.currentTimeMillis()));
            PushMsgInfoRO pushMsgInfoRO = new PushMsgInfoRO();
            pushMsgInfoRO.setUserCode(userCode);
            pushMsgInfoRO.setClientId(clientId);
            pushMsgInfoRO.setStatus(1);
            NoticePushRO noticePushRO = new NoticePushRO();
            noticePushRO.setTitle(title);
            noticePushRO.setBody(body);
            noticePushRO.setJumpPage(jumpPage);
            noticePushRO.setReceiver(openId);
            noticePushRO.setClientId(clientId);
            pushMsgInfoRO.setContent(JSONObject.toJSONString(noticePushRO));
            singlePushSendRequestRO.setMsgInfoRO(pushMsgInfoRO);
            pushMsgService.singlePush(singlePushSendRequestRO);
        } catch (Exception e) {
            log.error("singleNotice,push message exception", e);
        }
    }
}
